package homework.day03;
import jdbc.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


    public class Test3 {
    static Connection connection = null;

    static {
        try {
            connection = DBUtil.getConnection();
        } catch (SQLException e) {
           e.printStackTrace();
        }
    }

        public static void main(String[] args) throws SQLException {
            Test3 test3 = new Test3();
            test3.insert("张三而","三儿",15);
            test3.update("fdsa",2);
            test3.delete(3);
            test3.select();
        }

        public void insert(String username,String nickname,int age)  {


            try {
                String sql = "INSERT INTO userinfo (username,nickname,age) VALUES (?, ?,?)";
                PreparedStatement ps = connection.prepareStatement(sql);
                ps.setString(1, username);
                ps.setInt(2, age);
                ps.setString(3, nickname);
                int i = ps.executeUpdate();
                System.out.println(i > 0 ? "添加成功" : "添加失败");
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                DBUtil.close(connection);
            }

        }

        public void update(String password,int id)  {

            try {
                String sql = "UPDATE userinfo SET password = ? WHERE id = ?";
                PreparedStatement ps = connection.prepareStatement(sql);
                ps.setString(1,  password);
                ps.setInt(2, id);
                ps.executeUpdate();
                System.out.println("修改成功");
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                DBUtil.close(connection);
            }

        }

        public void delete(int id)  {

            try {
                String sql = "DELETE FROM userinfo WHERE id = ?";
                PreparedStatement ps = connection.prepareStatement(sql);
                ps.setInt(1, id);
                int num = ps.executeUpdate();
                System.out.println(num > 0 ? "删除成功" : "删除失败");
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                DBUtil.close(connection);
            }

        }

        public void select()  {

            try {
                String sql = "SELECT * FROM userinfo";
                PreparedStatement ps = connection.prepareStatement(sql);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    int id = rs.getInt("id");
                    String username = rs.getString("username");
                    String password = rs.getString("password");
                    int age = rs.getInt("age");
                    System.out.println(id +"--"+ username + "---"+password   + "--"+age);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                DBUtil.close(connection);
            }
        }
    }


